<?php
class Tourism_goods_attr_model extends BZ_Model
{
    protected $table = 'tourism_goods_attr';
    protected $table_2 = 'tourism_goods_base';
    protected $table_3 = 'user';

    public function page_list($page_num, $num, $param = array())
    {
        $this->db->select('b.*,u.alias_name,a.*');
        $this->db->from($this->table . ' as a');

        if (isset($param['good_name'])) {
            $this->db->like('b.good_name', trim($param['good_name']));
        }
        if (isset($param['alias_name'])) {
            $this->db->like('u.alias_name', trim($param['alias_name']));
        }
        if (isset($param['is_on_sale']) && $param['is_on_sale'] != 0) {
            $this->db->where('b.is_on_sale', (int)$param['is_on_sale']);
        }
        $this->db->join($this->table_2 . ' as b', 'a.goods_id = b.good_id');
        $this->db->join($this->table_3 . ' as u', 'u.uid = b.uid');

        $this->db->order_by('b.good_id', 'desc');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    public function getGoodsAttr($goods_attr_id)
    {
        $this->db->select('b.*,u.alias_name,a.*');
        $this->db->from($this->table . ' as a');
        $this->db->where('a.goods_attr_id', $goods_attr_id);
        $this->db->join($this->table_2 . ' as b', 'a.goods_id = b.good_id');
        $this->db->join($this->table_3 . ' as u', 'u.uid = b.uid');
        $this->db->limit(1);
        return $this->db->get()->row_array();
    }

    public function getGoodsQty($attr_id)
    {
        $this->db->where('goods_attr_id', $attr_id);
        return $this->db->get($this->table)->row();
    }

    public function add_cart($id, $qty)
    {
        $this->db->where('goods_attr_id', $id);
        $this->db->where('goods_num > ', $qty);
        return $this->db->get($this->table)->row();
    }

    /**
     * 添加
     * @param unknown $goods_id
     * @param unknown $postData
     */
    public function insertGoodsAttr($goods_id, $postData)
    {
        $data = array(
            'goods_id' => $goods_id,
            'market_price' => trim($postData['market_price']),
            'shop_price' => trim($postData['shop_price']),
            'supply_price' => trim($postData['supply_price']),
            'goods_num' => $postData['goods_num'],
            'weight' => $postData['weight'],
            'is_beizhu' => $postData['is_beizhu'],
            'is_check' => 2,
            'updated_at' => date('Y-m-d H:i:s', time()),
        );
        //商品规格 将属性规格保存
        if (!empty($postData['attribute'])) {
            $data['attribute_key'] = ',' . implode(',', $postData['attribute']) . ',';
            $data['attribute_value'] = $this->attribute($postData['attribute']);
        } else {
            $data['attribute_key'] = '';
            $data['attribute_value'] = '';
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 更新
     * @param unknown $goods_id
     * @param unknown $postData
     */
    public function updateGoodsAttr($goods_attr_id, $postData)
    {
        $data = array(
            'market_price' => trim($postData['market_price']),
            'shop_price' => trim($postData['shop_price']),
            'supply_price' => trim($postData['supply_price']),
            'goods_num' => $postData['goods_num'],
            'weight' => $postData['weight'],
            'is_check' => 2,
            'updated_at' => date('Y-m-d H:i:s', time()),
        );
        //商品规格 将属性规格保存
        if (!empty($postData['attribute'])) {
            $data['attribute_key'] = ',' . implode(',', $postData['attribute']) . ',';
            $data['attribute_value'] = $this->attribute($postData['attribute']);
        } else {
            $data['attribute_key'] = '';
            $data['attribute_value'] = '';
        }
        $this->db->where('goods_attr_id', $goods_attr_id);
        return $this->db->update($this->table, $data);
    }

    /**
     * 获取商品的SKU对应的规格
     * @param unknown $postData
     * @param string $str
     * @return string
     */
    public function attribute($postData = array(), $str = '')
    {
        foreach ($postData as $item) {
            $str[] = $this->tourism_goods_category_spec->getSpecNameById($item);
        }
        $result = implode(',', $str);
        return trim($result, ',');
    }
    
    /**
     * 
     * @param unknown $params
     */
    public function updateGoodsDelete($params)
    {
        $data = array(
                'logic_delete' => $params['logic_delete']
        );
        $this->db->where('goods_attr_id', $params['goods_attr_id']);
        return $this->db->update($this->table, $data);
    }

    /**
     * 返还库存
     * @param unknown $params
     */
    public function returnStock($params)
    {
        $this->db->where('goods_attr_id', $params['goods_attr_id']);
        $this->db->set('goods_num', 'goods_num+' . $params['refund_num'], false);
        return $this->db->update($this->table);
    }
}